package com.qqhf.hitfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qqhf.common.entity.AjaxResult;
import com.qqhf.common.entity.PageEntity;
import com.qqhf.hitfood.bean.core.BaseController;
import com.qqhf.hitfood.bean.entity.PosCity;
import com.qqhf.hitfood.service.IPosCityService;
import com.qqhf.hitfood.service.pro.PositionService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Api(tags = "P - 位置服务")
@RestController
@RequestMapping("/pos/city")
public class PosCityController extends BaseController {
    @Autowired
    private IPosCityService posCityService;
    @Autowired
    private PositionService positionService;

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, PosCity posCity) {
        return AjaxResult.success(posCityService.page(pageEntity.toPage(), Wrappers.lambdaQuery(posCity)));
    }

    @GetMapping("/list")
    public AjaxResult list(PosCity posCity) {
        return AjaxResult.success(posCityService.list(Wrappers.lambdaQuery(posCity)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody PosCity posCity) {
        return AjaxResult.success(posCityService.save(posCity));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PosCity posCity) {
        return AjaxResult.success(posCityService.updateById(posCity));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(posCityService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(posCityService.getById(id));
    }

    @Operation(summary = "地点搜索页显示城市列表")
    @GetMapping("/all")
    public Object cities(
            @Parameter(name = "type", description = "城市分类", required = true)
            @RequestParam("type") String type) {
        switch (type) {
            case "guess":
                return AjaxResult.success(posCityService.getOne(new QueryWrapper<PosCity>().eq("name", "威海"), false));
            case "hot":
                return AjaxResult.success(posCityService.list(new QueryWrapper<PosCity>().eq("initial", 23)));
            case "group":
                HashMap<String, List<PosCity>> map = new HashMap<>();
                char[] letter = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G',
                        'H', 'I', 'J', 'K', 'L', 'M', 'N',
                        'O', 'P', 'Q', 'R', 'S', 'T',
                        'U', 'V', 'W', 'X', 'Y', 'Z'};
                for (int i = 0; i < 26; i++) {
                    map.put(String.valueOf(letter[i]), posCityService.list(new QueryWrapper<PosCity>().eq("initial", i + 1)));
                }
                return AjaxResult.success(map);
            default:
                break;
        }
        return AjaxResult.error();
    }

    @Operation(summary = "根据城市、关键字获取地址")
    @GetMapping(value = "/pois/key")
    public Object getPoiByCityAndKeyword(
            @Parameter(name = "city_id", description = "城市id")
            @RequestParam(value = "city_id", required = false) Integer cityId,
            @Parameter(name = "keyword", description = "关键字", required = true)
            @RequestParam(value = "keyword") String keyword) {
        String cityName;
        if (cityId == null) {
            PosCity city = positionService.getPostion("120.224.102.189");
            cityName = city.getName();
        } else {
            PosCity byId = posCityService.getById(cityId);
            cityName = byId.getName();
        }
        return AjaxResult.success(positionService.searchPlace(cityName, keyword));
    }

    @Operation(summary = "根据经纬度坐标获取位置信息")
    @RequestMapping(value = "/pois/geohash", method = RequestMethod.GET)
    public Object getPoiByGeoHash(
            @Parameter(name = "geohash", description = "lat<纬度>,lng<经度>", required = true)
            @RequestParam("geohash") String geoHash) {
        System.out.println("geohash:" + geoHash);
        return AjaxResult.success(positionService.pois(geoHash));
    }
}
